﻿using ConsoleTableExt;
using IoTSharp.Data.Taos;
//using IoTSharp.ORM.Taos;
using System;
using System.ComponentModel;
using System.Data.Common;

namespace ORMExample
{

    //[Taos("BREAKER_BASIC")]//超级表名
    //[Description("时序表")]
    //public class EntityDemo
    //{
    //    /// <summary>
    //    /// 特性含义:别名,类型，类型长度，是否为Tag,是否为表名[表名只能设置一个字段]
    //    /// </summary>
    //    [TaosColumn("equipId", TaosDataType.BINARY, "64", true, true)]
    //    public string EquipId { get; set; }

    //    [TaosColumn("groupid", TaosDataType.INT, isTag: true)]
    //    public int GroupId { get; set; }

    //    [TaosColumn("current", TaosDataType.FLOAT)]
    //    public double Current { get; set; }

    //    [TaosColumn("voltage", TaosDataType.INT)]
    //    public int Voltage { get; set; }

    //    [TaosColumn("phase", TaosDataType.FLOAT)]
    //    public double Phase { get; set; }
    //}
    internal class Program
    {
        static void Main(string[] args)
        {
#if !NETFRAMEWORK
            System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

#endif
            //DbProviderFactories.RegisterFactory("TDengine", TaosFactory.Instance);
            string host = "172.8.10.125";
            short port = 6030;
            string username = "root";
            string password = "123456";
            string database = "db";
            var builder = new TaosConnectionStringBuilder()
            {
                DataSource = host,
                DataBase = database,
                Username = username,
                Password = password,
                Port = port,
                PoolSize=0
            };
            //Example for ADO.Net 
            using (var connection = new TaosConnection(builder.ConnectionString))
            {
                connection.Open();
                //Console.WriteLine("create {0} {1}", database, connection.CreateCommand($"create database {database};").ExecuteNonQuery());
                //Console.WriteLine("create table t {0} {1}", database, connection.CreateCommand($"create table {database}.t (ts timestamp, cdata int);").ExecuteNonQuery());
                //Console.WriteLine("insert into t values  {0}  ", connection.CreateCommand($"insert into {database}.t values ('{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ms")}', 10);").ExecuteNonQuery());
                //Console.WriteLine("insert into t values  {0} ", connection.CreateCommand($"insert into {database}.t values ('{DateTime.Now.AddMonths(1).ToString("yyyy-MM-dd HH:mm:ss.ms")}', 20);").ExecuteNonQuery());

                var cmd_select = connection.CreateCommand();
                cmd_select.CommandText = $"select * from {database}.t";
                using (var reader = cmd_select.ExecuteReader())
                {
                    //Console.WriteLine(cmd_select.CommandText);
                    //Console.WriteLine("");

                    //ConsoleTableBuilder.From(reader.ToDataTable()).WithFormat(ConsoleTableBuilderFormat.MarkDown).ExportAndWriteLine();
                    ConsoleTableBuilder.From(reader.ToDataTable()).WithFormat(ConsoleTableBuilderFormat.MarkDown).ExportAndWriteLine();
                }
                //Console.WriteLine("");
                //Console.WriteLine("DROP TABLE  {0} {1}", database, connection.CreateCommand($"DROP TABLE  {database}.t;").ExecuteNonQuery());
                //Console.WriteLine("DROP DATABASE {0} {1}", database, connection.CreateCommand($"DROP DATABASE   {database};").ExecuteNonQuery());
                connection.Close();
            }

            /////Specify the name of the database
            //string database = "db_" + DateTime.Now.ToString("yyyyMMddHHmmss");
            //var builder = new TaosConnectionStringBuilder()
            //{
            //    DataSource = "airleaderserver",
            //    DataBase = database,
            //    Username = "root",
            //    Password = "taosdata",
            //    Port = 6030

            //};
            //TaosOrm taosSugar = new TaosOrm(builder.ConnectionString);
            //_ = taosSugar.TaosAddAsync(
            //    new EntityDemo()
            //    {
            //        Current = 2,
            //        Phase = 30,
            //        Voltage = 4,
            //        EquipId = "3afbe36dd5b740a6b7c1634b70687d51",
            //        GroupId = 1
            //    }, true);
            Console.WriteLine("Hello World!");
        }
    }
}
